package com.yjf.ljr.activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.yjf.ljr.R;
import com.yjf.ljr.common.CommonData;
import com.yjf.ljr.connection.IjConnect;
import com.yjf.ljr.dataclass.ObtainCodeDataClass;
import com.yjf.ljr.dataclass.VerificatCodeDataClass;
import com.yjf.ljr.parser.RequestBuilder;
import com.yjf.ljr.parser.RequestBuilder.RequestObject;

public class MobileVerificationActivity extends IjActivity implements OnClickListener {

	@ID("tvMobile")
	private TextView mTvMobile;
	@ID("btObtainCode")
	private Button mBtObtainCode;
	@ID("btNext")
	private Button mBtNext;
	@ID("etMobileCode")
	private EditText mEtMobileCode;
	private TimeCount time;// 计时
	private String mobileCode = "";
	private String mobileNumber = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mobile_verification);
		initControl();
	}

	private void initControl() {
		setLeftBtnClick();
		setTitleStr("手机验证");
		Intent mGetMobile = getIntent();

		mobileNumber = mGetMobile.getStringExtra("obtain_mobile");
		String temp_mobile = mGetMobile.getStringExtra("obtain_mobile").subSequence(0, 3)
				+ "*****"
				+ mGetMobile.getStringExtra("obtain_mobile").substring(8,
						mGetMobile.getStringExtra("obtain_mobile").length());
		mTvMobile.setText(temp_mobile);
		mBtObtainCode.setOnClickListener(this);
		mBtNext.setOnClickListener(this);
		time = new TimeCount(60000, 1000);

	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.btObtainCode:
			if (!(TextUtils.isEmpty(mobileNumber))) {
				new ObtainCodeTask(mobileNumber).execute();
				time.start();
			}
			break;
		case R.id.btNext:
			mobileCode = mEtMobileCode.getText().toString();
			if (!(TextUtils.isEmpty(mobileCode))) {
				new VerificatCodeTask(mobileCode, mobileNumber).execute();
			} else {
				showToast("验证码不能为空！");
			}
			break;
		default:
			break;
		}
	}

	class ObtainCodeTask extends AsyncTask<Void, Void, String> {
		private String mobile;
		private String mFailStr;
		ObtainCodeDataClass dc = new ObtainCodeDataClass();

		public ObtainCodeTask(String mobile) {
			super();
			this.mobile = mobile;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(Void... arg0) {
			// TODO Auto-generated method stub
			RequestObject requestObject = new RequestObject();
			requestObject.method = "sendSmsCode";
			requestObject.map.put("mobile", mobile);
			requestObject.map.put("business", "ForgetLoginPassWord");
			// String requestbuild = new RequestBuilder().build(requestObject);

			return requestDataAndGetErrorMsg(requestObject, dc);
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if (!TextUtils.isEmpty(result)) {// fail
				dismissProgressDialog();
				// showToast(dc.msg);
				showToast(result);
			} else {// success
				dismissProgressDialog();
				showToast(dc.msg);
			}
		}

	}

	class TimeCount extends CountDownTimer {
		public TimeCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
		}

		@Override
		public void onFinish() {// 计时完毕时触发
			mBtObtainCode.setText("重新发送");
			mBtObtainCode.setClickable(true);
		}

		@Override
		public void onTick(long millisUntilFinished) {// 计时过程显示
			mBtObtainCode.setClickable(false);
			mBtObtainCode.setText(millisUntilFinished / 1000 + "秒重发");
			int temp = mBtObtainCode.getWidth();
			mBtObtainCode.setWidth(temp);
		}
	}

	class VerificatCodeTask extends AsyncTask<Void, Void, String> {
		private String code;
		private String mobile;
		VerificatCodeDataClass dc = new VerificatCodeDataClass();

		public VerificatCodeTask(String code, String mobile) {
			this.code = code;
			this.mobile = mobile;

		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(Void... arg0) {
			RequestObject requestObject = new RequestObject();
			requestObject.method = "verifySmsCode";
			requestObject.map.put("code", code);
			requestObject.map.put("business", "ForgetLoginPassWord");
			requestObject.map.put("mobile", mobile);
			return requestDataAndGetErrorMsg(requestObject, dc);
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub

			if (!(TextUtils.isEmpty(result))) {
				// fail
				showToast(result);
				return;
			} else {
				Intent intent = new Intent(mContext, SetPassWordActivity.class);
				intent.putExtra("obtain_code", code);
				startActivity(intent);
				finish();
			}
			super.onPostExecute(result);
		}

	}

}
